﻿Imports System.Windows.Forms

Public Class dlgSearchReplace

    'Link to Orignal Rich Text Box in Parent
    Private ctlRTBptr As RichTextBox
    'Start of the Search
    Private Start As Int32 = 0
    'Search term
    Private SearchTerm As String = ""

    Public Sub New(ByRef ctlRTB As RichTextBox)
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        ' Link our CtlRTBptr ( Control-RichTextBox-Pointer ) to the parent control
        CtlRTBptr = ctlRTB
    End Sub

    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
        TextBox2.Visible = CheckBox4.Checked
        Label1.Visible = CheckBox4.Checked
        btnReplace.Visible = CheckBox4.Checked
        btnReplace.Enabled = CheckBox4.Checked
    End Sub

    Private Sub dlgSearchReplace_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox2.Visible = False
        CheckBox4.Checked = False
    End Sub

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        If SearchTerm = Me.TextBox1.Text Then
            'If we've searched this before, keep the last start
        Else
            'Otherwise, start over
            Start = 0
        End If

        ' Make a Current Position Holder
        Dim Position As Int32
        ' Options Matrix for Searching Options
        Dim Options As RichTextBoxFinds = RichTextBoxFinds.None

        'Test for Checked Conditions and set the Optins With a Bitwise Or
        If CheckBox1.Checked = True Then Options = Options Or RichTextBoxFinds.WholeWord
        If CheckBox2.Checked = True Then Options = Options Or RichTextBoxFinds.MatchCase
        If CheckBox3.Checked = True Then Options = Options Or RichTextBoxFinds.Reverse

        'If there is a Selection already, then start there
        If ctlRTBptr.SelectionLength > 0 Then
            Start = ctlRTBptr.SelectionStart + ctlRTBptr.SelectionLength
            Position = ctlRTBptr.Find(TextBox1.Text, Start, Options)
        Else
            'Preform the Search
            Position = ctlRTBptr.Find(TextBox1.Text, Start, Options)
        End If


        'Set the start of the next search to that position if found
        If Position > 0 Then
            ctlRTBptr.Focus()
            'ElseIf 
        Else
            MsgBox("String Not Found", MsgBoxStyle.Critical)
            TextBox1.Focus()
        End If
    End Sub

    Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Start = 0
        Me.TextBox1.Text = ""
        Me.TextBox2.Text = ""
        Me.Hide()
    End Sub
End Class
